package com.demo.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.concurrent.Executor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author yufeng_ma@trtjk.com
 * @Description //TODO
 * @Date 2022/9/9 17:19
 **/
@Configuration(proxyBeanMethods = false)
public class NacosServiceDeregisterAutoConfig {

    @Bean
    public NacosServiceDeregisterEndpoint deregisterEndpoint(NacosAutoServiceRegistration registration) {
        return new NacosServiceDeregisterEndpoint(registration);
    }
    
    @Bean
    public Boolean registerListener(NacosConfigManager nacosConfigManager) throws NacosException {
    	String dataId = "nacosDemo";
        String group = "DEFAULT_GROUP";
    	ConfigService configService = nacosConfigManager.getConfigService();
    	Listener configListener = new Listener() {
            public Executor getExecutor() {
                return null;
            }

            public void receiveConfigInfo(String configInfo) {
                System.out.println("当前线程：" + Thread.currentThread().getName() + " ,监听到配置内容变化：" + configInfo);
            }
        };
        configService.addListener(dataId, group, configListener);
        return true;
    }
    
}
